package com.zy.springboot_nanhua4.controllers;

import lombok.extern.java.Log;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
@Log
public class GlobalExceptionHandler {

    @ExceptionHandler(RuntimeException.class)
    public ResponseEntity<ResponseResult> handleRuntimeException(RuntimeException ex){
        ex.printStackTrace();
        return new ResponseEntity<>(
                ResponseResult.error("服务器运行时出现错误，可能为参数类型错误："  + ex.getMessage()),
                HttpStatus.BAD_REQUEST);
    }

    @ExceptionHandler(Exception.class)
    public ResponseEntity<ResponseResult> handleAllException(Exception ex){
        ex.printStackTrace();
        return new ResponseEntity<>(
                ResponseResult.error("服务器出现致命错误："  + ex.getMessage()),
                HttpStatus.INTERNAL_SERVER_ERROR);
    }
}
